﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyCayXanh.Models;
using System.Data;
using System.Data.Entity;
using MvcPaging;

namespace QuanLyCayXanh.Controllers
{
    public class TreeController : Controller
    {
        private QuanLyCayXanhContext db = new QuanLyCayXanhContext();
        private const int defaultPageSize = 30;
        //
        // GET: /Tree/

        public ActionResult Index(int? page)
        {
            // set phan trang
            int currentPageIndex = page.HasValue ? page.Value : 1;
            IList<Tree> data = db.Trees.Include(t => t.Street).Include(t => t.FamilyTree).Include(t => t.GroupOfTree).Include(t => t.Ward).ToList();
            data = data.ToPagedList(currentPageIndex, defaultPageSize);
            return View(data);
        }

        public ActionResult Create()
        {
            ViewBag.FamilyTree_Id = new SelectList(db.FamilyTrees, "Id", "Name");
            ViewBag.GroupOfTree_Id = new SelectList(db.GroupOfTrees, "Id", "Name");
            ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");
            ViewBag.Ward_Id = null;
            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            return View();
        }

        //
        // POST: /Tree/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Tree tree)
        {
            if (ModelState.IsValid)
            {
                db.Trees.Add(tree);
                db.SaveChanges();
                return RedirectToAction("Index");
            }            
            ViewBag.FamilyTree_Id = new SelectList(db.FamilyTrees, "Id", "Name", tree.FamilyTree_Id);
            ViewBag.GroupOfTree_Id = new SelectList(db.GroupOfTrees, "Id", "Name", tree.GroupOfTree_Id);
            ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");
            ViewBag.Ward_Id = null;
            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            return View(tree);
        }

        //
        // GET: /Tree/Details/5

        public ActionResult Details(int id = 0)
        {
            Tree tree = db.Trees.Find(id);
            if (tree == null)
            {
                return HttpNotFound();
            }
            return PartialView(tree);
        }

        //
        // GET: /Tree/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Tree tree = db.Trees.Find(id);
            if (tree == null)
            {
                return HttpNotFound();
            }            
            if (tree.FamilyTree_Id != null)
                ViewBag.FamilyTree_Id = new SelectList(db.FamilyTrees, "Id", "Name", tree.FamilyTree_Id);
            else
                ViewBag.FamilyTree_Id = new SelectList(db.FamilyTrees, "Id", "Name");

            if (tree.GroupOfTree_Id != null)
                ViewBag.GroupOfTree_Id = new SelectList(db.GroupOfTrees, "Id", "Name", tree.GroupOfTree_Id);
            else
                ViewBag.GroupOfTree_Id = new SelectList(db.GroupOfTrees, "Id", "Name");

            if (tree.Street_Id != null)
                ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name", tree.Street_Id);
            else
                ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");

            Ward ward = db.Wards.Find(tree.Ward_Id);
            if (tree.Ward != null)
            {
                ViewBag.Ward_Id = new SelectList(db.Wards.Where(m => m.District_Id == ward.District_Id), "Id", "Name", tree.Ward.Id);
                ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name", tree.Ward.District.Id);
            }
            else
            {
                ViewBag.Ward_Id = null;
                ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            }
            return View(tree);
        }

        //
        // POST: /Tree/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Tree tree)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tree).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            if (tree.FamilyTree_Id != null)
                ViewBag.FamilyTree_Id = new SelectList(db.FamilyTrees, "Id", "Name", tree.FamilyTree_Id);
            else
                ViewBag.FamilyTree_Id = new SelectList(db.FamilyTrees, "Id", "Name");

            if (tree.GroupOfTree_Id != null)
                ViewBag.GroupOfTree_Id = new SelectList(db.GroupOfTrees, "Id", "Name", tree.GroupOfTree_Id);
            else
                ViewBag.GroupOfTree_Id = new SelectList(db.GroupOfTrees, "Id", "Name");

            if (tree.Street_Id != null)
                ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");
            else
                ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");

            if (tree.Ward != null)
            {
                ViewBag.Ward_Id = new SelectList(db.Wards, "Id", "Name", tree.Ward.Id);
                ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name", tree.Ward.District.Id);
            }
            else
            {
                ViewBag.Ward_Id = null;
                ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            }
            
            return View(tree);
        }

        //
        // GET: /Tree/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Tree tree = db.Trees.Find(id);
            if (tree == null)
            {
                return HttpNotFound();
            }
            return View(tree);
        }

        //
        // POST: /Tree/Delete/5

        [HttpPost, ActionName("Delete")]        
        public ActionResult DeleteConfirmed(int id)
        {
            Tree tree = db.Trees.Find(id);
            db.Trees.Remove(tree);
            db.SaveChanges();
            return Content(Boolean.TrueString);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        [HttpPost]
        public ActionResult _GetSelectWard(string District_Id)
        {
            int id = int.Parse(District_Id);
            var wards = db.Wards.Where(s => s.District_Id == id).ToList();
            ViewBag.Ward_Id = new SelectList(wards, "Id", "Name");
            return PartialView("_SelectWard");
        }

    }
}
